--This function was stolen from Simply Love
local function getRandomBackgrounds(filePath)
    -- Allow users to artbitrarily add new judgment graphics to /Graphics/_judgments/
    -- without needing to modify this script;
    -- instead of hardcoding a list of judgment fonts, get directory listing via FILEMAN.
    --local path = THEME:GetPathG("",filePath)
    local files = FILEMAN:GetDirListing(filePath.."/")
    local backgrounds = {}
    local backgroundsLength=0 --Fucking lua

    for k,filename in ipairs(files) do

		--Fix this later
        --local name = filename:gsub(".jpg", "")
        backgrounds[#backgrounds+1] = filename
        backgroundsLength = backgroundsLength + 1;
        -- A user might put something that isn't a suitable judgment graphic
        -- into /Graphics/_judgments/ (also sometimes hidden files like .DS_Store show up here).
        -- Do our best to filter out such files now.
        --[[if string.match(filename, " %dx%d") then
            -- use regexp to get only the name of the graphic, stripping out the extension
            local name = filename:gsub(" %dx%d", ""):gsub(".png", "")
            backgrounds[#backgrounds+1] = name
            backgroundsLength = backgroundsLength+1;
        end]]
    end
	--If no backgrounds were found
	if backgroundsLength < 1 then return nil end;
    --PrintTable(backgrounds);
    local bg = backgrounds[math.random(backgroundsLength)]
    Warn(filePath.."/"..bg)
    return filePath.."/"..bg
end;
local bestGrade;

if _G.themeDebug ~= true then
  bestGrade = STATSMAN:GetBestGrade();
else
  bestGrade = _G.DebugGrade --Lua, being moronic as always, starts at 1 and not 0.
end;
local gradeLetter = getGradeLetter(bestGrade);--Return the numerical grade returned by the game into a human readable one (A, AA, etc)

local song = GAMESTATE:GetCurrentSong();

return Def.ActorFrame{
    --===================
    -- Background
    --===================
    Def.Sprite{
        InitCommand=function(self)
			local gradeImg;
			if bestGrade < 4 then
				if STATSMAN:GetCurStageStats():GetEarnedExtraStage() == "EarnedExtraStage_Extra1" or _G.DebugGoToExtra then
					gradeImg = getRandomBackgrounds(THEME:GetPathG("","_result images/EX_entry"));
				elseif STATSMAN:GetCurStageStats():GetEarnedExtraStage() == "EarnedExtraStage_Extra2" or _G.DebugGoToOMES then
					gradeImg = getRandomBackgrounds(THEME:GetPathG("","_result images/OMES_entry"));
				else
					gradeImg = getRandomBackgrounds(THEME:GetPathG("","_result images/"..gradeLetter.."_result"));
				end;
				if gradeImg ~= nil then
					--SCREENMAN:SystemMessage(gradeImg);
					self:Load(gradeImg);
				end;
			end;
			--Check if song exists because it needs debug compatibility
			--if gradeImg is nil that means there isn't a unique background for AAA,AA,A
			if song and gradeImg == nil then
				self:Load(GetSongBackground());
				--self:Cover();
			--if 
			elseif gradeImg == nil then
				self:Load(THEME:GetPathG("common", "fallback background"));
				--self:Cover();
			end;
			self:scaletocover(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
        end;
    };
    
    --[[Def.Quad{
        InitCommand=cmd(diffuse,color("0,0,0,.8");setsize,SCREEN_WIDTH/4,SCREEN_HEIGHT;horizalign,right;xy,SCREEN_WIDTH,SCREEN_CENTER_Y;fadeleft,.5);
	};
	Def.Quad{
        InitCommand=cmd(diffuse,color("0,0,0,.8");setsize,SCREEN_WIDTH/2,SCREEN_HEIGHT;horizalign,left;xy,0,SCREEN_CENTER_Y;);
	};
	Def.Quad{
        InitCommand=cmd(diffuse,color("0,0,0,.8");setsize,SCREEN_WIDTH/4,SCREEN_HEIGHT;horizalign,left;xy,SCREEN_CENTER_X,SCREEN_CENTER_Y;faderight,.5);
	};]]
	Def.Quad{
        InitCommand=cmd(diffuse,color("0,0,0,.8");setsize,SCREEN_WIDTH/2,SCREEN_HEIGHT;horizalign,right;xy,SCREEN_WIDTH,SCREEN_CENTER_Y;fadeleft,1);
	};
	Def.Quad{
        InitCommand=cmd(diffuse,color("0,0,0,.8");setsize,SCREEN_WIDTH/2,SCREEN_HEIGHT;horizalign,left;xy,0,SCREEN_CENTER_Y;faderight,1);
	};
};
